package com.jic.product.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.ProductBrand;
import com.jic.product.manage.request.ProductBrandEditRequest;
import com.jic.product.manage.request.ProductBrandRequest;
import com.jic.product.manage.response.ProductBrandResponse;
import com.jic.product.service.ProductBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZW on 2020/1/9 11:04
 * 商品品牌管理
 */
@RestController
@RequestMapping("/productBrand")
@Api(value = "商品设置", description = "品牌管理", tags = "商品管理")
@Slf4j
public class ProductBrandController {

    @Autowired
    private ProductBrandService productBrandService;

    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取所有商品品牌信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<ProductBrandResponse>> queryPage(@RequestBody ProductBrandRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        ProductBrand productBrand = BeanUtil.copy(form, ProductBrand.class);
        PageResult<ProductBrand> result = productBrandService.queryPage(productBrand, page);
        List<ProductBrandResponse> productBrandResponseList = result.getRows().stream().map(temp -> {
            ProductBrandResponse productBrandResponse = BeanUtil.copy(temp, ProductBrandResponse.class);
            return productBrandResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<ProductBrandResponse> pageResult = new PageResult<>();
        pageResult.setRows(productBrandResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }

    /*
     *  商品品牌新增、修改
     *
     * */
    @ResponseBody
    @PostMapping(value = "/editProductBrand")
    public RestResult insertProductBrand(@RequestBody ProductBrandEditRequest productBrandEditRequest) {
        log.info("------商品品牌新增、修改{}-----", productBrandEditRequest.toString());
        RestResult restResult = new RestResult<>();
        try {
            //修改
            if (productBrandEditRequest.getId() != 0) {
                productBrandEditRequest.setUpdater(productBrandEditRequest.getAdminOperate().getOperator());
                long flag = productBrandService.update(productBrandEditRequest);
                restResult = RestResult.success(flag);
            } else {
                productBrandEditRequest.setDeptId(productBrandEditRequest.getAdminOperate().getDeptId());
                productBrandEditRequest.setCreator(productBrandEditRequest.getAdminOperate().getOperator());
                long flag = productBrandService.insert(productBrandEditRequest);
                restResult = RestResult.success(flag);
            }

        } catch (Exception e) {
            log.error("insertProductBrand============>" + e);
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }


    /*
     *  商品品牌删除
     *
     * */
    @ResponseBody
    @PostMapping(value = "/deleteProductBrand")
    public RestResult deleteProductBrand(Long id) {
        log.info("------商品品牌新增、修改{}-----", id);
        RestResult restResult = new RestResult<>();
        try {
            long flag = productBrandService.delete(id);
            if (flag == 1) {
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            log.error("deleteProductBrand============>" + e);
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;

    }

    /*
     *  商品品牌条件查询
     *
     * */
    @ResponseBody
    @PostMapping(value = "/queryProductBrand")
    public RestResult query(@RequestBody ProductBrandRequest productBrandRequest) {
        log.info("------商品品牌新增、修改{}-----", productBrandRequest.toString());
        productBrandRequest.setDeptId(productBrandRequest.getAdminOperate().getDeptId());
        RestResult restResult = new RestResult();
        List<ProductBrandResponse> productBrandResponses = null;
        try {
            productBrandResponses = productBrandService.query(productBrandRequest);
            restResult.setData(productBrandResponses);
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("query============>" + e);
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }

        return restResult;
    }

    /*
     *  商品品牌分页查询
     * */
    @ResponseBody
    @PostMapping(value = "/queryBrandList")
    public RestResult queryBrandList(@RequestBody ProductBrandRequest productBrandRequest) {
        log.info("------商品品牌分页查询-----", productBrandRequest.toString());
        productBrandRequest.setDeptId(productBrandRequest.getAdminOperate().getDeptId());
        RestResult restResult = new RestResult();
        try {
            PageResult<ProductBrandResponse> productBrandResponsePageResult = productBrandService.queryList(productBrandRequest);;
            restResult.setData(productBrandResponsePageResult);
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("query============>" + e);
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }

        return restResult;
    }

    @ResponseBody
    @PostMapping(value = "/querySourceList")
    @ApiOperation(value = "获取商品品牌中的所有品牌来源")
    public RestResult querySourceList() {
        return productBrandService.querySourceList();
    }



}
